package com.xuan.web;
import com.alibaba.druid.util.StringUtils;
import com.xuan.dao.GameDao;
import com.xuan.model.Game;
import com.xuan.service.GameService;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet(urlPatterns = "/servlet/Acc_DetailsServiceAction")
@MultipartConfig(maxFileSize = 10*1024*1024)
@SuppressWarnings("all")
public class Acc_DetailsServiceAction extends HttpServlet {
    private GameDao gameDao = new GameDao();
    private GameService service = new GameService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String act = request.getParameter("act");
        if (act == null) {
            act = "list";
        }
        Game game = getServiceTypeFromRequest(request);
        switch (act) {
            case "addSave":
                String msg = checkServiceType(game);
                if (!"".equals(msg)) {
                    request.setAttribute("act","addSave");
                    request.setAttribute("game",game);
                    forward(request,response,msg,"/WEB-INF/jsp/serviceDetails1/input.jsp");
                }
                if (service.add(game)) {
                    forward(request,response,"保存成功","Acc_DetailsServiceTypeAction?act=list");
                }else {
                    request.setAttribute("act","addSave");
                    request.setAttribute("game",game);
                    forward(request,response,"保存失败","/WEB-INF/jsp/serviceDetails1/input.jsp");
                }
                break;
            case "updateSave":
                if (service.update(game)) {
                    forward(request,response,"修改成功","Acc_DetailsServiceTypeAction?act=list");
                }else {
                    request.setAttribute("act","addSave");
                    request.setAttribute("game",game);
                    forward(request,response,"修改失败","/WEB-INF/jsp/serviceDetails1/input.jsp");
                }
                break;
            default:
                int page = 1;
                String size = getServletContext().getInitParameter("pageSize");
                String page1 = request.getParameter("page");
                if (page1 != null) {
                    page = Integer.parseInt(page1);
                }
            request.setAttribute("page",page);
//                request.setAttribute("games",gameDao.getAll());
                request.setAttribute("lastpage",service.getPageSize(Integer.parseInt(size)));
                forward(request,response,"acc_details",
                        service.getAll(Integer.parseInt(size),page),"/WEB-INF/jsp/serviceDetails1/list.jsp");
        }
    }

    private String checkServiceType(Game game) {
        String msg = "";
        if (StringUtils.isEmpty(game.getTitle())) {
            return "请输入游戏名";
        }
        return msg;
    }

    private Game getServiceTypeFromRequest(HttpServletRequest request) throws IOException, ServletException {
        Game game = new Game();
        game.setId(Integer.parseInt(request.getParameter("id")));
//        String uploadDir = getServletContext().getInitParameter("uploadDir");
        String realPath = getServletContext().getInitParameter("imgPath");
//        String realPath = getServletContext().getRealPath(uploadDir);
        File file = new File(realPath);
        if (!file.exists()) {
            file.mkdirs();
        }
        Part acc_img = request.getPart("icon");
        if (acc_img != null && acc_img.getSize() > 0) {
            String submitFileName = acc_img.getSubmittedFileName();
            String fileName = coverFileName(submitFileName);
            game.setIcon("/img/"+fileName);
            System.out.println(realPath+"\\"+fileName);
            acc_img.write(realPath+"\\"+fileName);
        }
        String title = request.getParameter("title");
        if (title != null) {
            game.setTitle(title);
        }
        return game;
    }

    private String coverFileName(String submitFileName) {
        DateFormat dateFormat = new SimpleDateFormat("-yyyyMMddhhmmss");
        String temp = dateFormat.format(new Date());
        int pos = submitFileName.lastIndexOf(".");
        return submitFileName.substring(0,pos)+temp+submitFileName.substring(pos);
    }

    private void forward(HttpServletRequest request, HttpServletResponse response, String key,Object value, String url) throws ServletException, IOException {
        request.setAttribute(key, value);
        request.getRequestDispatcher(url).forward(request, response);
    }
    private void forward(HttpServletRequest request, HttpServletResponse response, String msg, String url) throws ServletException, IOException {
        request.setAttribute("msg", msg);
        request.getRequestDispatcher(url).forward(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }
}
